字符串类型在在在论何语言都是很重要的，JavaScript中的字符串替换功能要用到正则表达式，必须熟练掌握。
新建字符串
几乎所有类型都自动转换为字符串。字符串既可以用String类来生成，也可以用各种类型的对象的toString()方法来转换。
xx = "abc";
xx = new String("abd"); //String类
yy = 123;
xx = yy.toString(); //Integer类的toString()方法
yy = new Date(2004,11,25);
xx = yy.toString(); //Date类的toString()方法
xx = "abc" + 123 + "xyz"; //123自动转换为字符串
转意字符
以下的转意字符要注意。例：在对话框中输出2行文字：alert('你好！\n欢迎光临。');
\n - 换行
\r - 回车
\f - フォームフィード
\b - 退格字符
\t - Tab字符
\' - 单引号（'）
\" - 双引号（"）
\\ - 反斜杠（\）
\nnn - 8进制（例 "A" 写成： "\101"）
\xnn - 16进制（例 "A" 写成： \x41"）
\unnnn - Unicode字符（例 "あ" 写成： "\u3042"）
字符串的长度
string.length //取字符串的长度，汉字和英数字都作为1个Unicode字符。
xx = "abc五笔字型".length; //xx的值为: 7
xx = "abc\n五笔字型".length; //xx的值为: 8
取字符串的部分内容
string.charAt(n) //取第n个字符
xx = "abcd".charAt(2) //xx的值为: "c"
xx = "静坐常思自己过".charAt(2) //xx的值为: "常"
string.substring(from,to) //取从from到to的字符串（不包含第to个字符）
string.substring(from) //取从from开始到最后的字符串
//注：负数当作0处理，超过长度时，超过部分忽略不计。
dd = "静坐常思自己过".substring(2) //xx的值为: "常思自己过"
dd = "静坐常思自己过".substring(0,2) //xx的值为: "静坐"
dd = "静坐常思自己过".substring(-5,2) //xx的值为: "静坐"
dd = "静坐常思自己过".substring(0,-2) //xx的值为: ""
dd = "静坐常思自己过".substring(2，20) //xx的值为: "常思自己过"
dd = "静坐常思自己过".substring(2，-7) //xx的值为: "静坐"
string.substr(from, len) //取从from的len个字符串
//注：from是负数时，则从右向左取数。len为负数时作0处理。
dd = "静坐常思自己过".substr(2) //xx的值为: "常思自己过"
dd = "静坐常思自己过".substr(1,3) //xx的值为: "坐常思"
dd = "静坐常思自己过".substr(-2) //xx的值为: "己过"
dd = "静坐常思自己过".substr(1,12) //xx的值为: "坐常思自己过"
dd = "静坐常思自己过".substr(1,-2) //xx的值为: ""
string.slice(from [, to])
//注：当from为0或正数时，与substring完全相同。from为0或负数时，则从右向左取数。
dd = "静坐常思自己过".substring(2) //xx的值为: "常思自己过"
dd = "静坐常思自己过".substring(-1，3) //xx的值为: "自己"
字符串的分割与连接
string.split(sep [,limit]) //将字符串按sep分割limit次。
//注：sep不指定时，返回整个字符串，limit为负数时，等同不指定。
xx = "aa:bb:cc:dd:ee:ff".split(":"); //结果： xx[0] = "aa"; xx[1] = "bb"; xx[2] = "cc";
// xx[3] = "dd"; xx[4] = "ee"; xx[5] = "ff";
xx = "aa:bb:cc:dd:ee:ff".split(":", 2); //结果： xx[0] = "aa"; xx[1] = "bb";
xx = "aa:bb:cc:dd:ee:ff".split(":", -2); //结果： xx = "", 等同于split(":",0)。
string.concat(string2) //连接字符串，与+功能相同。
xx = "谦受益".concat("满招损"); //等价于: xx = "谦受益"+"满招损";
字符串的查找
string.indexOf(key[,from]) //从from个字符开始查找字符串按key。
//注：若找不到，则返回-1。找到则返回所在的位置。
xx = "abcdefABCDEF".indexOf("CD"); //xx的值为: 8
xx = "abcdefABCDEF".indexOf("cd",2); //xx的值为: 2
xx = "abcdefABCDEF".indexOf("cd",3); //xx的值为: -1
string.lastIndexOf(key[,from]) //从from个字符开始向左查找字符串按key。
//注：若找不到，则返回-1。找到则返回所在的位置。
xx = "abcdefabcdef".lastIndexOf("cd"); //xx的值为: 8
xx = "abcdefabcdef".lastIndexOf("cd",7); //xx的值为: 2
xx = "abcdefabcdef".lastIndexOf("cd",0); //xx的值为: -1
字符串的替换
string.replace(regexp,newString) //string的内容中与regexp正则表达式相符的部分替换成newString。
//注：必须具备正则表达式的知识。
xx = "abcdefABCDEF".replace("def","xyz"); //xx的值为: "abcxyzABCDEF"
string.toUpperCase() //英文大写
string.toLowerCase() //英文小写
xx = "abcdefABCDEF".toUpperCase(); //xx的值为: ABCDEFABCDEF
xx = "abcdefABCDEF".toLowerCase(); //xx的值为: abcdefabcdef
正则表达式的使用
string.match(regexp) //返回与正则表达式匹配的字符串
xx = "abcdefABCDEF".match(/def/i); //xx的值为: "def"
xx = "abcdefABCDEF".match(/xyz/i); //xx的值为: null
string.match(regexp) //返回与正则表达式匹配的字符串的位置，找不到返回-1
xx = "abcdefABCDEF".match(/def/i); //xx的值为: 3
xx = "abcdefABCDEF".match(/xyz/i); //xx的值为: -1
字符串与字符的变换
string.charCodeAt(n) //取第n个字符
xx = "abcdefABCDEF".charCodeAt(2); //xx的值为: "c"
xx = "abcdefABCDEF".charCodeAt(-1); //xx的值为: NaN
xx = "abcdefABCDEF".charCodeAt(20); //xx的值为: NaN
String.fromCharCode(num1, ... , numN) //将N个字符转换为字符串
xx = String.fromCharCode(0x41, 0x42, 0x43); //xx的值为: "ABC"
xx = String.fromCharCode("A", "B", 0x43); //JavaScript出错
字符串与其它类型的变换
ParseInt(string) //字符串转换为整型
ParseFloat(string) //字符串转换为浮点型
Eval(string) //取第n个字符
xx = "abcdefABCDEF".charCodeAt(2); //xx的值为: "c"
xx = "abcdefABCDEF".charCodeAt(-1); //xx的值为: NaN
xx = "abcdefABCDEF".charCodeAt(20); //xx的值为: NaN
String.fromCharCode(num1, ... , numN) //将N个字符转换为字符串
xx = String.fromCharCode(0x41, 0x42, 0x43); //xx的值为: "ABC"
xx = String.fromCharCode("A", "B", 0x43); //JavaScript出错
字符串转换成带Tag修饰
string.bold() //<b>string</b>
string.italics() //<i>string</i>
string.fixed() //<tt>string</tt>
string.big() //<big>string</big>
string.small() //<small>string</small>
string.blink() //<blink>string</blink>
string.strike() //<strike>string</strike>
string.sup() //<sup>string</sup>
string.sub() //<sub>string</sub>
string.fontcolor(color) //<font color=color>string</font>
string.fontsize(size) //<font size=size>string</font>
string.anchor(name) //<a name=name>string</a>
string.link(name) //<a href=name>string</a>
xx = "abcdefABCDEF".fixed(); //xx的值为: "<fix>abcdefABCDEF</fix>"
